using System;
using System.Text;
using System.IO;
using System.Collections;

namespace Calendar {
    public enum ClassificationType {PUBLIC,PRIVATE,CONFIDENTIAL}

    /// <summary>
    /// Summary description for vEvent.
    /// </summary>
    public class vEvent {
        private DateTime _dateCreated = DateTime.Now;
        private DateTime _endDateTime;
        private DateTime _startDateTime;
        private string _location;
        public System.Collections.Generic.List<string> Categories = new System.Collections.Generic.List<string>();
        public ClassificationType Classification = ClassificationType.PUBLIC;
           
        private int _priority = 0;
  
        private string _subject = "";
        private string _description = "";
           
        #region Public Constructors
        public vEvent() {
  
        }
        public vEvent(string subject, string description, DateTime startDateTime, DateTime endDateTime) {
            _subject = subject;
            _description = description;
            _startDateTime = startDateTime;
            _endDateTime = endDateTime;
        }
  
        public vEvent(string subject, string description, DateTime startDateTime, DateTime endDateTime, int priority) {
            _subject = subject;
            _description = description;
            _priority = priority;
            _startDateTime = startDateTime;
            _endDateTime = endDateTime;
        }
  
        public vEvent(string subject, string description, DateTime startDateTime, TimeSpan meetingLength) {
            _subject = subject;
            _description = description;
            _startDateTime = startDateTime;
            _endDateTime = startDateTime.Add(meetingLength);
        }
  
        public vEvent(string subject, string description, DateTime startDateTime, TimeSpan meetingLength, int priority) {
            _subject = subject;
            _description = description;
            _priority = priority;
            _startDateTime = startDateTime;
            _endDateTime = startDateTime.Add(meetingLength);
        }
  
        #endregion


        public void ParseEvent(string Event) {
            string lineDiv = "\n";
            //if(Event.IndexOf("\r\n") > 0) lineDiv = "\r\n";
            string[] items = System.Text.RegularExpressions.Regex.Split(Event, lineDiv);
            foreach(string item in items) {
                if(item.Length > 0) {
                    int colonPosition = item.IndexOf(":");
                    if(colonPosition > 0) {
                        string prefix = item.Substring(0, colonPosition);
                        string value = item.Substring(colonPosition + 1);
                        if(value.EndsWith("\r")) value = value.Substring(0, value.Length - 1);
                        string mask = "yyyyMMddTHHmmss";
                        if(value.EndsWith("Z")) mask = mask + "Z";
                        switch(prefix) {
                            case "DCREATED":
                                _dateCreated = DateTime.ParseExact(value, mask, null).ToLocalTime();
                                break;
                            case "DTSTART":
                                _startDateTime = DateTime.ParseExact(value,mask, null).ToLocalTime();
                                break;
                            case "DTEND":
                                _endDateTime = DateTime.ParseExact(value, mask, null).ToLocalTime();
                                break;
                            case "PRIORITY":
                                int.TryParse(value, out _priority);
                                break;
                            case "LOCATION":
                                _location = value;
                                break;
                            case "SUMMARY":
                                _subject = value;
                                break;
                            case "CATEGORIES":
                                string[] catlist = value.Split(';');
                                Categories.AddRange(catlist);
                                break;
                            case "CLASS":
                                if(value == ClassificationType.CONFIDENTIAL.ToString()) Classification = ClassificationType.CONFIDENTIAL;
                                if(value == ClassificationType.PRIVATE.ToString()) Classification = ClassificationType.PRIVATE;
                                if(value == ClassificationType.PUBLIC.ToString()) Classification = ClassificationType.PUBLIC;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }

        public override string ToString() {
            StringBuilder sb = new StringBuilder();
               
            //Start the event
            sb.Append("BEGIN:VEVENT\r\n");
               
            //Add the date created
            sb.Append("DCREATED:" + _dateCreated.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + "\r\n");
               
            sb.Append("DTSTART:" + _startDateTime.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + "\r\n");
            sb.Append("DTEND:" + _endDateTime.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + "\r\n");

            sb.Append("PRIORITY:" + _priority + "\r\n");
            sb.Append("LOCATION:" + _location + "\r\n");
  
            sb.Append("SUMMARY:" + _subject + "\r\n");
            sb.Append("DESCRIPTION:" + _description + "\r\n");
 
            if( Categories.Count != 0 ) {
                sb.Append("CATEGORIES:");
                foreach(string category in Categories) {
                    sb.Append( category + ";" );
                }
                sb.Append( "\r\n" );
            }
 
            sb.Append("CLASS:" + Enum.GetName(typeof(ClassificationType), Classification) + "\r\n" );
 
            //End the event
            sb.Append("END:VEVENT\r\n");
 
            return sb.ToString();
        }
          
        /// <summary>
        /// DateTime that the vEvent was created. Not required. Defaults to current date and time.
        /// </summary>
        public DateTime DateCreated {
            get {
                return _dateCreated;
            }
 
            set {
                _dateCreated = value;
            }
        }
 
        /// <summary>
        /// DateTime that the vEvent will be done.
        /// </summary>
        public DateTime EndDateTime {
            get {
                return _endDateTime;
            }
 
            set {
                _endDateTime = value;
            }
        }
 
        /// <summary>
        /// DateTime that the vEvent will start.
        /// </summary>
        public DateTime StartDateTime {
            get {
                return _startDateTime;
            }
 
            set {
                _startDateTime = value;
            }
        }
 
        /// <summary>
        /// Any valid Int32. 0 is undefined. 1 is High Priority. -1 is Low Priority. Defaults to 0.
        /// </summary>
        public int Priority {
            get {
                return _priority;
            }
 
            set {
                _priority = value;
            }
        }
 
        /// <summary>
        /// Subject of the vEvent
        /// </summary>
        public string Subject {
            get {
                return _subject;
            }
 
            set {
                _subject = value;
            }
        }
 
        /// <summary>
        /// Description of the vEvent
        /// </summary>
        public string Description {
            get {
                return _description;
            }

            set {
                _description = value;
            }
        }
        /// Description of the vEvent
        /// </summary>
        public string Location {
            get {
                return _location;
            }

            set {
                _location = value;
            }
        }
    }
 
}